package com.qinge.blog.service.impl;

import com.qinge.blog.dao.BlogContentDao;
import com.qinge.blog.dao.BlogTagDao;
import com.qinge.blog.entity.*;
import com.qinge.blog.service.BlogContentService;
import com.qinge.blog.service.BlogTagService;
import com.qinge.blog.vo.BlogContentVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.dao.BlogContentTagDao;
import com.qinge.blog.service.BlogContentTagService;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogContentTag service层实现类
 */
@Service
public class BlogContentTagServiceImpl  implements BlogContentTagService {
    @Resource
    BlogContentTagDao dao;

    @Resource
    BlogContentDao contentDao;

    @Resource
    BlogTagDao tagDao;

    @Override
    public void add(BlogContentTag... contentTags) {
        dao.batchInsert(Arrays.asList(contentTags));
    }

    @Override
    public void addByList(List<BlogContentTag> contentTags) {
        dao.batchInsert(contentTags);
    }

    @Override
    public void removeByConIds(String...conIds) {
      removeByConIdList(Arrays.asList(conIds));
    }

    @Override
    public void removeByTagIds(String ...tagIds) {
        removeByTagIdList(Arrays.asList(tagIds));
    }

    @Override
    public void removeByConIdList(List<String> conIds) {
        BlogContentTagExample example=new BlogContentTagExample();
        BlogContentTagExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdIn(conIds);

        dao.deleteByExample(example);
    }

    @Override
    public void removeByTagIdList(List<String> tagIds) {
        BlogContentTagExample example=new BlogContentTagExample();
        BlogContentTagExample.Criteria criteria=example.createCriteria();

        criteria.andTagIdIn(tagIds);

        dao.deleteByExample(example);
    }

    @Override
    public List<BlogContent> findContentsByTag(String tagId) {
        BlogContentTagExample example=new BlogContentTagExample();
        BlogContentTagExample.Criteria criteria=example.createCriteria();

        criteria.andTagIdEqualTo(tagId);
        List<BlogContentTag> blogContentTags = dao.selectByExample(example);

        List<String> conIds=blogContentTags.stream().map(BlogContentTag::getContentId).collect(Collectors.toList());

        BlogContentExample contentExample=new BlogContentExample();
        BlogContentExample.Criteria contentCriteria=contentExample.createCriteria();

        contentCriteria.andIdIn(conIds);

        return  contentDao.selectByExample(contentExample);
    }


    @Override
    public int findCountByTag(String tagId) {
        BlogContentTagExample example=new BlogContentTagExample();
        BlogContentTagExample.Criteria criteria=example.createCriteria();

        criteria.andTagIdEqualTo(tagId);
        List<BlogContentTag> blogContentTags = dao.selectByExample(example);

        List<String> conIds=blogContentTags.stream().map(BlogContentTag::getContentId).collect(Collectors.toList());

        return  conIds.size();
    }
    @Override
    public List<BlogTag> findTagsByConId(String conId) {
        BlogContentTagExample example=new BlogContentTagExample();
        BlogContentTagExample.Criteria criteria=example.createCriteria();

        criteria.andContentIdEqualTo(conId);

        List<BlogContentTag> blogContentTags = dao.selectByExample(example);

        List<String> tagIds = blogContentTags.stream().map(BlogContentTag::getTagId).collect(Collectors.toList());

        BlogTagExample tagExample=new BlogTagExample();
        BlogTagExample.Criteria tagCriteria = tagExample.createCriteria();

        if(tagIds.size()>0){
            tagCriteria.andIdIn(tagIds);
        }

        return tagDao.selectByExample(tagExample);
    }
}

